package com.ztgf.sms.api.client.sms;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ztgf.common.constant.Result;
import com.ztgf.common.constant.ZTConstant;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.AESUtil;
import com.ztgf.common.utils.RandomValidateCodeUtil;
import com.ztgf.common.utils.SignUtils;
import com.ztgf.sms.models.bo.SmsInfoBO;
import com.ztgf.sms.models.bo.SmsInfoOtherSaveBO;
import com.ztgf.sms.models.bo.SmsInfoSaveBO;
import com.ztgf.sms.models.bo.SmsInfoSaveCaptchaBO;
import com.ztgf.sms.models.service.ISmsInfoService;
import com.ztgf.sms.models.vo.SmsResTemplteVO;
import com.ztgf.sms.models.vo.SmsResVo;
import com.ztgf.sys.models.dto.SysUserDTO;
import com.ztgf.sys.models.vo.SysUserKeyVO;
import com.ztgf.user.models.service.IUserLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 短信对外接口类
 *
 * @author zhanghaulang
 * @Date 2020-05-20
 */
@RestController
@RequestMapping("/api/sms")
@Slf4j
@Api(value = "SmsInfoApiController",tags = "短信服务接口")
public class SmsInfoApiController {
    @DubboReference
    private ISmsInfoService smsInfoService;
    @DubboReference
    private IUserLoginService userLoginService;

    @ApiOperation(value = "发送普通短信(对接玄武科技或成都创信)",notes = "发送普通短信(对接玄武科技或成都创信)")
    @PostMapping(value = "/save")
    public Result save( SmsInfoSaveBO smsInfoSaveBO) throws Exception{
        smsInfoService.save(smsInfoSaveBO);
        return new Result<>();
    }

    @ApiOperation(value = "发送注册通过审核短信(对接玄武科技或成都创信)",notes = "发送注册通过审核短信(对接玄武科技或成都创信)")
    @PostMapping(value = "/passAudit")
    public Result passAudit( SmsInfoSaveBO smsInfoSaveBO) throws Exception{
        if (StringUtils.isEmpty(smsInfoSaveBO.getMobile())){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        smsInfoService.savePassAudit(smsInfoSaveBO);
        return new Result<>();
    }

    @ApiOperation(value = "发送注册不通过审核结果短信(对接玄武科技或成都创信)",notes = "发送注册不通过审核短信(对接玄武科技或成都创信)")
    @PostMapping(value = "/unPassAudit")
    public Result unPassAudit( SmsInfoSaveBO smsInfoSaveBO) throws Exception{
        if (StringUtils.isEmpty(smsInfoSaveBO.getMobile())){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        smsInfoService.saveUnPassAudit(smsInfoSaveBO);
        return new Result<>();
    }

    @ApiOperation(value = "发送开店通过审核结果短信(对接玄武科技或成都创信)",notes = "发送开店通过审核短信(对接玄武科技或成都创信)")
    @PostMapping(value = "/passOpenAudit")
    public Result passOpenAudit( SmsInfoSaveBO smsInfoSaveBO) throws Exception{
        if (StringUtils.isEmpty(smsInfoSaveBO.getMobile())){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        smsInfoService.savePassOpenAudit(smsInfoSaveBO);
        return new Result<>();
    }

    @ApiOperation(value = "发送开店不通过审核结果短信(对接玄武科技或成都创信)",notes = "发送开店不通过审核短信(对接玄武科技或成都创信)")
    @PostMapping(value = "/unPassOpenAudit")
    public Result unPassOpenAudit( SmsInfoSaveBO smsInfoSaveBO) throws Exception{
        if (StringUtils.isEmpty(smsInfoSaveBO.getMobile())){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        smsInfoService.saveUnPassOpenAudit(smsInfoSaveBO);
        return new Result<>();
    }

    @ApiOperation(value = "发送资质变更审核结果短信(对接玄武科技或成都创信)",notes = "发送资质变更审核结果短信(对接玄武科技或成都创信)")
    @PostMapping(value = "/qualificationChange")
    public Result qualificationChange( SmsInfoSaveBO smsInfoSaveBO) throws Exception{
        if (StringUtils.isEmpty(smsInfoSaveBO.getMobile())){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        smsInfoService.saveQualificationChange(smsInfoSaveBO);
        return new Result<>();
    }

    @ApiOperation(value = "发送IM提示短信(对接玄武科技或成都创信)",notes = "发送IM提示短信(对接玄武科技或成都创信)")
    @PostMapping(value = "/im")
    public Result im( SmsInfoSaveBO smsInfoSaveBO) throws Exception{
        if (StringUtils.isEmpty(smsInfoSaveBO.getMobile())){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        smsInfoService.saveIM(smsInfoSaveBO);
        return new Result<>();
    }

    @ApiOperation(value = "发送验证码(对接玄武科技或成都创信) ",notes = "发送验证码(对接玄武科技或成都创信)")
    @PostMapping(value = "/saveCaptcha")
    public Result saveCaptcha( SmsInfoSaveCaptchaBO smsInfoSaveCaptchaBO) throws Exception{
        smsInfoService.saveCaptcha(smsInfoSaveCaptchaBO);
        return new Result<>();
    }

    @ApiOperation(value = "发送验证码(脱敏手机号) ",notes = "脱敏手机号")
    @PostMapping(value = "/sendCaptcha")
    public Result sendCaptcha( SmsInfoSaveCaptchaBO smsInfoSaveCaptchaBO) throws Exception{
        smsInfoService.sendCaptcha(smsInfoSaveCaptchaBO);
        return new Result<>();
    }

    @ApiOperation(value = "发送验证码(对接玄武科技或成都创信)验证图形 ",notes = "发送验证码(对接玄武科技或成都创信)验证图形")
    @PostMapping(value = "/saveCaptchaVa")
    public Result saveCaptchaVa( SmsInfoSaveCaptchaBO bo) throws Exception{

        String saveCode = userLoginService.getVerifyWidth(RandomValidateCodeUtil.DEFAULT_CODE_KEY + bo.getRandomStr());

        if (StringUtils.isEmpty(saveCode)) {
            return new Result<>(CodeEnum.FAIL_Login_1008);
        }
        //误差在10px以内
        Integer saveCodeInt1 = Integer.parseInt(saveCode) - 10;
        Integer saveCodeInt2 = Integer.parseInt(saveCode) + 10;
        Integer codeInt = Integer.parseInt(bo.getCaptcha());

        if (saveCodeInt1 <= codeInt && saveCodeInt2 >= codeInt) {
            userLoginService.deleteVerifyWidth(RandomValidateCodeUtil.DEFAULT_CODE_KEY + bo.getRandomStr());
        } else {
            userLoginService.deleteVerifyWidth(RandomValidateCodeUtil.DEFAULT_CODE_KEY + bo.getRandomStr());
            return new Result<>(CodeEnum.FAIL_Login_1008);
        }
        smsInfoService.saveCaptcha(bo);
        return new Result<>();
    }

    @ApiOperation(value = "发送验证码(对接玄武科技或成都创信)验证图形 ",notes = "发送验证码(对接玄武科技或成都创信)验证图形")
    @PostMapping(value = "/saveCaptchaVaWX")
    public Result saveCaptchaVaWX( SmsInfoSaveCaptchaBO bo) throws Exception{
        smsInfoService.saveCaptcha(bo);
        return new Result<>();
    }

    @ApiOperation(value = "发送验证码(对接阿里) ",notes = "发送验证码(对接阿里)")
    @PostMapping(value = "/saveALCaptcha")
    public Result saveALCaptcha( SmsInfoSaveCaptchaBO smsInfoSaveCaptchaBO) throws Exception{
        smsInfoService.saveALCaptcha(smsInfoSaveCaptchaBO);
        return new Result<>();
    }

    @ApiOperation(value = "验证验证码",notes = "验证验证码")
    @PostMapping(value = "/getCaptcha")
    public Result getCaptcha( SmsInfoBO smsInfoBO){
        if (StringUtils.isEmpty(smsInfoBO.getMobile()) || StringUtils.isEmpty(smsInfoBO.getContent()) || smsInfoBO.getCaptchaType() ==  null){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean flag = smsInfoService.getCaptcha(smsInfoBO);

        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL);
        }
    }

    @ApiOperation(value = "其他平台发送普通短信(对接成都创信)",notes = "其他平台发送普通短信(对接成都创信)")
    @PostMapping(value = "/send")
    public Result senddc( SmsInfoOtherSaveBO bo) throws Exception{
        if (bo.getId() == null || StringUtils.isEmpty(bo.getContent()) || StringUtils.isEmpty(bo.getMobile())){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return smsInfoService.saveOtherDc(bo);
    }

    @ApiOperation(value = "获取通道资费(成都创信和玄武)",notes = "获取通道资费(成都创信和玄武)")
    @PostMapping(value = "/getAisleInfo")
    public Result getAisleInfo() throws Exception{
        return smsInfoService.getAisleInfo();
    }

    @ApiOperation(value = "其他平台发送普通短信(对接成都创信,玄武，阿里)",notes = "其他平台发送普通短信(对接成都创信,玄武，阿里)")
    @PostMapping(value = "/sendByPass")
    public Result sendByPass( SmsInfoOtherSaveBO bo) throws Exception{
        if (bo.getId() == null || StringUtils.isEmpty(bo.getContent()) || StringUtils.isEmpty(bo.getMobile()) || StringUtils.isEmpty(bo.getCompanyName())){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return smsInfoService.saveOtherPass(bo);
    }


    @ApiOperation(value = "SSO统一认证获取登录手机号",notes = "SSO统一认证获取登录手机号")
    @PostMapping(value = "/smsSend")
    public Result smsSend(String mobile) {

        String res = smsInfoService.smsSend(mobile);
        SmsResVo smsRes = JSONUtil.toBean(res, SmsResVo.class);
        if (smsRes.getCode()==0){
            return new Result<>(CodeEnum.SUCCESS);
        }else {
            return new Result<>(CodeEnum.FAIL_100002);
        }
    }

    @ApiOperation(value = "业务中台获取短信模板集合",notes = "业务中台获取短信模板集合")
    @PostMapping("/messageTemplte")
    public Result messageTemplte() throws Exception {
        log.info("messageTemplte-------------------------start");
            int i = RandomUtils.nextInt(1000000, 10000000);
            String nonceStr = "" + System.currentTimeMillis() + i;
            String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
            String sign = SignUtils.createSign(ZTConstant.ZT_CONFIG_APPID, timestamp, nonceStr, ZTConstant.ZT_CONFIG_APPID, ZTConstant.ZT_CONFIG_SMS_SECRET);
            try {
                String res = HttpUtil.createGet(ZTConstant.ZT_CONFIG_MESSAGE_PATH)
                        .header("appId", ZTConstant.ZT_CONFIG_APPID)
                        .header("nonce", nonceStr)
                        .header("timestamp", timestamp)
                        .header("sign", sign)
                        .execute().body();
                Object code = JSONObject.parseObject(res).get("code");
                log.info("res-------------------------"+res);
                log.info("code-------------------------"+code);
                if (ObjectUtil.equals(code, 0)) {
                    SmsResTemplteVO smsResTemplteVO = JSONUtil.toBean(res, SmsResTemplteVO.class);
                    log.info("smsResTemplteVO-------------------------"+smsResTemplteVO.toString());
                    return new Result<>(smsResTemplteVO.getData());
                }
            } catch (Exception e) {
                e.printStackTrace();
                return new Result<>(CodeEnum.FAIL);
            }
        return new Result<>(CodeEnum.FAIL);

    }
}
